package com.tanhua.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.exception.BusinessException;
import com.tanhua.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import model.domain.UserInfo;
import model.enums.CommentType;
import model.mongo.Comment;
import model.vo.CommentVo;
import model.vo.ErrorResult;
import model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CommentsService {
    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public PageResult findComments(String movementId, Integer page, Integer pagesize) {
        List<Comment> comments = commentApi.findComments(movementId, CommentType.COMMENT, page, pagesize);

        if (CollUtil.isEmpty(comments)) {
            return new PageResult();
        }

        List<Long> userIds = CollUtil.getFieldValues(comments, "userId", Long.class);

        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);

        ArrayList<CommentVo> commentVos = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfo userInfo = map.get(comment.getUserId());
            if (userIds != null) {
                CommentVo commentVo = CommentVo.init(userInfo, comment);
                commentVos.add(commentVo);
            }
        }

        return new PageResult(page, pagesize, 0L, commentVos);
    }

    public void saveComments(String movementId, String comment) {
        Long userId = UserHolder.getUserId();

        Comment comment1 = new Comment();
        comment1.setPublishId(new ObjectId(movementId));
        comment1.setCommentType(CommentType.COMMENT.getType());
        comment1.setContent(comment);
        comment1.setUserId(userId);
        comment1.setCreated(System.currentTimeMillis());

        Integer commentCount = commentApi.save(comment1);
        log.info("commentCount=" + commentCount);
    }

    public Integer likeComment(String movementId) {
        boolean hasComment = commentApi.hasComment(movementId, UserHolder.getUserId(), CommentType.LIKE);

        if (hasComment) {
            throw new BusinessException(ErrorResult.likeError());
        }

        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(CommentType.LIKE.getType());
        comment.setUserId(UserHolder.getUserId());
        comment.setCreated(System.currentTimeMillis());
        Integer count = commentApi.save(comment);

        String key = Constants.MOVEMENTS_INTERACT_KEY + movementId;
        String hashKey = Constants.MOVEMENT_LIKE_HASHKEY + UserHolder.getUserId();
        redisTemplate.opsForHash().put(key, hashKey, "1");
        return count;
    }

    public Integer dislikeComment(String movementId) {
        boolean hasComment = commentApi.hasComment(movementId, UserHolder.getUserId(), CommentType.LIKE);

        if (!hasComment) {
            throw new BusinessException(ErrorResult.dislikeError());
        }

        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(CommentType.LIKE.getType());
        comment.setUserId(UserHolder.getUserId());
        Integer count = commentApi.delete(comment);

        String key = Constants.MOVEMENTS_INTERACT_KEY + movementId;
        String hashKey = Constants.MOVEMENT_LIKE_HASHKEY + UserHolder.getUserId();
        redisTemplate.opsForHash().delete(key, hashKey);
        return count;
    }

    public Integer loveComment(String movementId) {
        boolean hasComment = commentApi.hasComment(movementId, UserHolder.getUserId(), CommentType.LOVE);

        if (hasComment) {
            throw new BusinessException(ErrorResult.loveError());
        }

        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(CommentType.LOVE.getType());
        comment.setUserId(UserHolder.getUserId());
        comment.setCreated(System.currentTimeMillis());
        Integer count = commentApi.save(comment);

        String key = Constants.MOVEMENTS_INTERACT_KEY + movementId;
        String hashKey = Constants.MOVEMENT_LOVE_HASHKEY + UserHolder.getUserId();
        redisTemplate.opsForHash().put(key, hashKey, "1");
        return count;
    }

    public Integer unloveComment(String movementId) {
        boolean hasComment = commentApi.hasComment(movementId, UserHolder.getUserId(), CommentType.LOVE);

        if (!hasComment) {
            throw new BusinessException(ErrorResult.unloveError());
        }

        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(CommentType.LOVE.getType());
        comment.setUserId(UserHolder.getUserId());
        Integer count = commentApi.delete(comment);

        String key = Constants.MOVEMENTS_INTERACT_KEY + movementId;
        String hashKey = Constants.MOVEMENT_LOVE_HASHKEY + UserHolder.getUserId();
        redisTemplate.opsForHash().delete(key, hashKey);
        return count;


    }
}














